<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>购物车</title>
</head>
	<style>
		*{
			margin:0;
			padding:0;
			list-style-type:none;
		}
		a{
			color:#666;
			text-decoration:none;
		}
		table{
			border-collapse:collapse;
			border-spacing:0;
			border:0;
		}

		.fl{
			float:left;
		}
		.fr{
			float:right;
		}
		.catbox{
			width:940px;
			margin:100px auto;
		}
		.catbox table{
			text-align:center;
			width:100%;
		}
		.catbox table th,.catbox table td{
			border:1px solid #CADEFF;
		}
		.catbox table td{
			padding:20px;
			color:#444;
		}
		.check-all{
			 vertical-align:middle;
		}
		.goods{
			width:300px;
		}
		.goods span{
			width:180px;
			margin-top:20px;
			text-align:left;
			float:left;
		}
		.goods img{
			width:100px;
			height:80px;
			margin-right:10px;
			float:left;
		}
		.count .add, .count input, .count .reduce{
			float:left;
			margin-right:-1px;
			position:relative;
			z-index:0;
		}
		.count .add, .count .reduce{
			height:23px;
			width:17px;
			border:1px solid #e5e5e5;
			background:#f0f0f0;
			text-align:center;
			line-height:23px;
			color:#444;
		}
		.count input{
			width:50px;
			height:25px;
			line-height:15px;
			border:1px solid #aaa;
			color:#343434;
			text-align:center;
			padding:4px 0;
			background-color:#fff;
			z-index:2;
		}
		.subtotal{
			width:150px;
			color:red;
			font-weight:bold;
		}
		.operation span:hover,a:hover{
			cursor:pointer;
			color:red;
			text-decoration:underline;
		}
		
		.foot{
			margin-top:0px;
			color:#666;
			height:48px;
			border:1px solid #c8c8c8;
			border-top:0;
			background-color:#eaeaea;
			/* background-image:linear-gradient(RGB(241,241,241),RGB(226,226,226)); */
			position:relative;
			z-index:8;}
		.foot div, .foot a{
			line-height:48px;
			height:48px;
		}
		.foot .select-all{
			width:80px;
			height:48px;
			line-height:48px;
			color:#666;
		    text-align:center;
		}
		.foot .delete{
			padding-left:10px;
		}
		.foot .closing{
			border-left:1px solid #c8c8c8;
			width:103px;text-align:center;
			color:#666;
			font-weight:bold;
			cursor:pointer;
			background-image:linear-gradient(RGB(241,241,241),RGB(226,226,226));}
		.foot .closing:hover{
			background-image:linear-gradient(RGB(226,226,226),RGB(241,241,241));
			color:#333;
		}
		.foot .total{
			margin:0 20px;
			cursor:pointer;
		}
		.foot  #priceTotal, .foot #selectedTotal{
			color:red;
			font-family:"Microsoft Yahei";
			font-weight:bold;
		}
	
	</style>
	<script>
	  window.onload = function () {
      // 这个函数并没有执行
      if (!document.getElementsByClassName) {
          alert("执行了");
          // getElementsByClassName:获取所有指定的类名
          document.getElementsByClassName = function (cls) {
              console.log(cls);  //check    check-all
              var ret = [];
              var els = document.getElementsByTagName('*');
              console.log(els);  //长度是104
              for (var i = 0, len = els.length; i < len; i++) {
              // indexof()方法可返回某个指定的字符串中首次出现的位置
               if (els[i].className.indexOf(cls + ' ') >=0 || els[i].className.indexOf(' ' + cls + ' ') >=0 || els[i].className.indexOf(' ' + cls) >=0) {
                      ret.push(els[i]);
                  }
              }
              console.log(ret);
              return ret;
          }
       
      }

 
      var table = document.getElementById('cartTable'); // 购物车表格
      var selectInputs = document.getElementsByClassName('check'); // 所有勾选框
      var checkAllInputs = document.getElementsByClassName('check-all') // 全选框
      var tr = table.children[1].rows; //行
      var selectedTotal = document.getElementById('selectedTotal'); //已选商品数目容器
      var priceTotal = document.getElementById('priceTotal'); //总计
      var deleteAll = document.getElementById('deleteAll'); // 删除全部按钮
      var selectedViewList = document.getElementById('selectedViewList'); //浮层已选商品列表容器
      var selected = document.getElementById('selected'); //已选商品
      var foot = document.getElementById('foot');
 
      // 更新总数和总价格，已选浮层
      function getTotal() {
          var seleted = 0;
          var price = 0;
          var HTMLstr = '';
          for (var i = 0, len = tr.length; i < len; i++) {
              //通过for循环，遍历到某一行，则通过getElementsByTagName('input')[0].checked，
              // 这句话选中这个商品
              if (tr[i].getElementsByTagName('input')[0].checked) {
                  //为这个商品设置类名on
                  tr[i].className = 'on';
                  //拿到商品的数量之后，在原来已经选中的商品中继续叠加，叠加到商品总的数量上
                  seleted += parseInt(tr[i].getElementsByTagName('input')[1].value);
                   // 拿到选中商品的对应的价格，叠加到商品总额中
                  price += parseFloat(tr[i].cells[4].innerHTML);
                  // 把获取到的商品图片添加到浮层中
                  HTMLstr += '<div><img src="' + tr[i].getElementsByTagName('img')[0].src + '"><span class="del" index="' + i + '">取消选择</span></div>'
              }
              else {
                  // 如果没有选中该行商品，则将没有选中的类名置为空
                  tr[i].className = '';
              }
          }
          selectedTotal.innerHTML = seleted;
          
          // 如果省略了该参数，将用0代替。
          priceTotal.innerHTML = price.toFixed(2);
        //   selectedViewList.innerHTML = HTMLstr;
          // 如果没有选中任何商品，则还是默认的状态
          if (seleted == 0) {
              foot.className = 'foot';
          }
      }
      // 计算单行价格
      // 把每一行的标签tr当做参数
      function getSubtotal(tr) {
          console.log(tr);//打印出来的是对应的行标签tr里面所包含的所有的内容
          var cells = tr.cells;//把每一种商品中的所有属性赋值给cells
          var price = cells[2]; //单价 ，把每一种商品的第二个属性即，价格，赋值给price
          var subtotal = cells[4]; //小计td，解释同上
          // 选中商品数量对应的input标签，赋值给countInput
          var countInput = tr.getElementsByTagName('input')[1]; //数目input
          // console.log(tr.getElementsByTagName('input')[1]);
          var span = tr.getElementsByTagName('span')[1]; //-号
          //写入HTML
          subtotal.innerHTML = (parseInt(countInput.value) * parseFloat(price.innerHTML)).toFixed(2);
          //如果数目只有一个，把-号去掉
          if (countInput.value == 1) {
              span.innerHTML = '';
          }else{
              span.innerHTML = '-';
          }
      }
 
      // 点击选择框
      for(var i = 0; i < selectInputs.length; i++ ){
          selectInputs[i].onclick = function () {
              if (this.className.indexOf('check-all') >= 0) { //如果是全选，则吧所有的选择框选中
 
                  console.log(this.className.indexOf('check-all'));//打印的是0
                    //selectInputs.length=6
                  for (var j = 0; j < selectInputs.length; j++) {
                      //6
                      selectInputs[j].checked = this.checked;  //全部选中复选框
                      //这里的this是指所有的<input class=​"check-all check" type=​"checkbox">​
 
                  }
              }
              if (!this.checked) { //只要有一个未勾选，则取消全选框的选中状态
                  for (var i = 0; i < checkAllInputs.length; i++) {
                      checkAllInputs[i].checked = false;
                  }
              }
              //再次调用getTotal()函数，迭代更新
              getTotal();//选完更新总计
          }
      }
 
  
      //为每行元素添加事件
      for (var i = 0; i < tr.length; i++) {
          //将点击事件绑定到tr元素
          tr[i].onclick = function (e) {
              var e = e || window.event;
              var el = e.target || e.srcElement; //通过事件对象的target属性获取触发元素
              var cls = el.className; //触发元素的class名
              console.log(cls);   //add /reduce
              //这里的this是指tr标签
              var countInout = this.getElementsByTagName('input')[1]; // 数目input
              var value = parseInt(countInout.value); //数目
              //通过判断触发元素的class确定用户点击了哪个元素
              switch (cls) {
                  case 'add': //点击了加号
                      countInout.value = value + 1;
                      getSubtotal(this);
                      break;
                  case 'reduce': //点击了减号
                      if (value > 1) {
                          countInout.value = value - 1;
                          getSubtotal(this);  //this指的是tr标签
                      }
                      break;
                  case 'delete': //点击了删除
                      var conf = confirm('确定删除此商品吗？');
                      if (conf) {
                          this.parentNode.removeChild(this);  //this指的是tr标签
                      }
                      break;
              }
              // 更新总数和总价格
              getTotal();
          }
          // 给数目输入框绑定keyup事件
          tr[i].getElementsByTagName('input')[1].onkeyup = function () {
              var val = parseInt(this.value);  //是数字，number类型的
              console.log(this.value); //<input class="count-input" type="text" value="1">
              //判断输入框内只能是大于零的整数，不能是除数字之外的其他非法字符
              if (isNaN(val) || val <= 0) {
                  val = 1;
              }
              // 下面这句话的意思是防止用户在手动输入商品数量的时候出现小数，即使出现小数
              // 也会取整的
              if (this.value != val) {
                  this.value = val;
              }
              getSubtotal(this.parentNode.parentNode); //更新小计
              console.log(this.parentNode.parentNode); //指的是tr标签
              getTotal(); //更新总数
          }
      }
      // 点击全部删除
      deleteAll.onclick = function () {
          if (selectedTotal.innerHTML != 0) {
              var con = confirm('确定删除所选商品吗？'); //弹出确认框
              if (con) {
                  for (var i = 0; i < tr.length; i++) {
                      // 如果被选中，就删除相应的行
                      if (tr[i].getElementsByTagName('input')[0].checked) {
                          tr[i].parentNode.removeChild(tr[i]); // 删除相应节点
                          i--; //回退下标位置
                      }
                  }
              }
          } else {
              alert('请选择商品！');
          }
          getTotal(); //更新总数
      }

      // 默认全选
      checkAllInputs[0].checked = true;
      checkAllInputs[0].onclick(this);  //这里的this是指的是window
      console.log(this);
  }
  </script>
<body>
    <div class="catbox">
    <!-- 使用table标签制作整个购物车的结构和样式 -->
    <table id="cartTable">
        <thead>
        <tr>
            <th><label>
                <input class="check-all check" type="checkbox">  全选</label></th>
            <th>商品信息</th>
            <th>单价</th>
            <th>数量</th>
            <th>金额</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <!-- 为每一行商品信息-->
        <tr class="on">
            <td class="checkbox"><input class="check-one check" type="checkbox"></td>
            <td class="goods">
				<img src="https://dss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1375532681,2838930656&fm=26&gp=0.jpg" alt="">
				<span>苹果</span>
			</td>
            <td class="price">5.88</td>
            <td class="count">
				<span class="reduce">-</span>
                <input class="count-input" type="text" value="1">
                <span class="add">+</span></td>
            <td class="subtotal">5.88</td>
            <td class="operation"><span class="delete">删除</span></td>
        </tr>
        <tr class="on">
            <td class="checkbox"><input class="check-one check" type="checkbox"></td>
            <td class="goods">
				<img src="https://dss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=135896716,3704103640&fm=26&gp=0.jpg" alt="">
				<span>香蕉</span>
			</td>
            <td class="price">3.50</td>
            <td class="count">
				<span class="reduce">-</span>
                <input class="count-input" type="text" value="1">
                <span class="add">+</span></td>
            <td class="subtotal">3.50</td>
            <td class="operation"><span class="delete">删除</span></td>
        </tr>
        <tr class="on">
            <td class="checkbox"><input class="check-one check" type="checkbox"></td>
            <td class="goods">
				<img src="https://dss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2984740582,1039576067&fm=26&gp=0.jpg" alt="">
				<span>草莓</span>
			</td>
            <td class="price">14.00</td>
            <td class="count">
				<span class="reduce">-</span>
                <input class="count-input" type="text" value="1">
                <span class="add">+</span></td>
            <td class="subtotal">14.00</td>
            <td class="operation"><span class="delete">删除</span></td>
        </tr>
        <tr class="on">
            <td class="checkbox"><input class="check-one check" type="checkbox"></td>
            <td class="goods">
				<img src="https://dss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1618568227,2677001747&fm=26&gp=0.jpg" alt="">
				<span>西瓜</span>
			</td>
            <td class="price">6.60</td>
            <td class="count">
				<span class="reduce">-</span>
                <input class="count-input" type="text" value="1">
                <span class="add">+</span></td>
            <td class="subtotal">6.60</td>
            <td class="operation"><span class="delete">删除</span></td>
        </tr>
        </tbody>
    </table>
    <!-- 制作购物车表格下面的结构样式 -->
    <div class="foot" id="foot">
        <label class="fl select-all"><input type="checkbox" class="check-all check">  全选</label>
        <a class="fl delete" id="deleteAll" href="javascript:;">删除</a>
        <div class="fr closing" onclick="getTotal();">结 算</div>
        <!-- 隐藏input标签   -->
        <input type="hidden" id="cartTotalPrice">
 
        <div class="fr total">合计：￥<span id="priceTotal">11957.48</span></div>
        <div class="fr selected" id="selected">已选商品<span id="selectedTotal">4</span>件</div>
    </div>
</div>
</body>
</html>